// pages/Protocol/ProtocolPreview/ProtocolPreview.js
import { queryUserSignature , addUserIou , queryUserIou } from '../../../api/protocol';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    protocol: {},
    lendUserSignatureImgUrl: '',
    borrowUserSignatureImgUrl: '',
    show: false,
    signList: [],
    swiperIndex: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.protocol){
      var protocol = JSON.parse(options.protocol)
      this.setData({
        protocol: protocol,
      })
    }
    this._queryUserSignature();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this._queryUserSignature();
    setTimeout(()=>{
      wx.stopPullDownRefresh({
        success: (res) => {
          console.log(res,'刷新成功')
          wx.showToast({
            title: '刷新成功',
            icon: 'none',
          })
        },
      })
    },1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let userId = wx.getStorageSync('userId')
    return {
      path: '/pages/Welcome/Welcome?scene='+userId,
    }
  },
  // 获取用户签名列表信息
  _queryUserSignature(){
    let _this = this;
    queryUserSignature({})
    .then(res => {
      if(res.success){
        var list = res.data;
        list.push({'isAdd': '1'});
        _this.setData({
          signList: list,
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 页面跳转
  goNext(e){
    let _this = this;
    let type = e.target.dataset.type||e.currentTarget.dataset.type;
    let url = e.target.dataset.name||e.currentTarget.dataset.name;
    let item = e.target.dataset.item||e.currentTarget.dataset.item;
    // return
    if(item){
      if(type == 1){
        wx.switchTab({
          url: `${url}`,
        })
      }else{
        wx.navigateTo({
          url: `${url}?item=${JSON.stringify(item)}`,
        })
      }
    }else{
      if(type == 1){
        wx.switchTab({
          url: `${url}`,
        })
      }else{
        wx.navigateTo({
          url: `${url}`,
          events: {
            'updateSignature': function(data){
              _this._queryUserSignature();
            }
          }
        })
      }
    }
  },
  // 展示选择签名弹框
  showSignSel(){
    this.setData({show: true});
  },
  onClose(){
    this.setData({show: false,});
  },
  // 切换签名轮播图
  swiperChange(event){
    console.log(event)
    this.setData({swiperIndex: event.detail.current})
  },
  submit(){
    let _this = this;
    if(_this.data.swiperIndex == _this.data.signList.length - 1){
      wx.showToast({
        title: '请选择签名后再进行操作',
        icon: 'none',
      });
      return;
    }
    var iou = _this.data.protocol;
    if(iou.isLend == '1'){
      _this.data.lendUserSignatureImgUrl = _this.data.signList[_this.data.swiperIndex].signatureImgUrl;
      iou['lendUserSignatureImgUrl'] = _this.data.signList[_this.data.swiperIndex].signatureImgUrl;
    }else{
      _this.data.borrowUserSignatureImgUrl = _this.data.signList[_this.data.swiperIndex].signatureImgUrl;
      iou['borrowUserSignatureImgUrl'] = _this.data.signList[_this.data.swiperIndex].signatureImgUrl;
    }
    console.log(iou,'借条')
    addUserIou(iou)
    .then(res => {
      if(res.success){
        queryUserIou({page: 0,size: 1,createUserId: wx.getStorageSync('userId')})
        .then(res => {
          if(res.success){
            wx.navigateTo({
              url: `/pages/Protocol/ProtocolUserSuccess/ProtocolUserSuccess?protocolId=${res.data.content[0].id}&isLend=${_this.data.protocol.isLend}`,
            })
          }
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none'
        })
      }
    })
  },
})